//
//  CPSCGoodsDetailsServiceDetailsView.m
//  CPSCustomer
//
//  Created by codew on 7/29/21.
//  Copyright © 2021 dgg_hzk. All rights reserved.
//

#import "CPSCGoodsDetailsServiceDetailsView.h"
#import "CPSCGoodsDetailsUIHelper.h"

#define KValueFontLblContent 15
#define KValueMaxWidthLblContent (ScreenWidth - CPSCGoodsDetailsUIHelper.marginLeft * 2)



@interface CPSCGoodsDetailsServiceDetailsView ()


@property (nonatomic, strong) UIView * viewHtmlContentSuper;
@property (nonatomic, strong) UILabel * lblContent;

@end

@implementation CPSCGoodsDetailsServiceDetailsView

- (instancetype)initWithFrame:(CGRect)frame
{
    if(self = [super initWithFrame:frame]){
        
        [self initSubviews];
    }
    return self;
}


- (void)initSubviews
{
    [self addHeaderView];
    
    [self addSubview:self.viewHtmlContentSuper];
    [self.viewHtmlContentSuper mas_makeConstraints:^(MASConstraintMaker *make) {
        
        CGFloat marginValue = [CPSCGoodsDetailsUIHelper marginLeft];
        make.top.mas_equalTo([CPSCGoodsDetailsUIHelper heightCommonHeader]);
        make.bottom.mas_equalTo(-marginValue);
        make.left.mas_equalTo(marginValue);
        make.right.mas_equalTo(-marginValue);
    }];
    
    
    [self deBugUI];
}



- (void)addHeaderView
{
    
}

- (void)updateViewWithHtmlString:(NSString *)htmlString
{
    
    NSMutableAttributedString *htmlAttributedStr = [self attStringWithHtmlStr:htmlString];
    
    self.lblContent.attributedText = htmlAttributedStr;
    self.viewHeight = [self heightViewWithHtmlAttributedStr:htmlAttributedStr];
    
    self.blockHeightUpdate();
}

- (NSMutableAttributedString *)attStringWithHtmlStr:(NSString *)html
{
    html = [NSString stringWithFormat:@"<head><style>img{width:%f !important;height:auto}</style></head>%@", KValueMaxWidthLblContent,html];
    html = [NSString stringWithFormat:@"<html><head></head><body>%@<br /></body></html>", html];;
    
    NSMutableAttributedString *htmlAttributedStr =
    [[NSMutableAttributedString alloc]
     initWithData:[html dataUsingEncoding:NSUTF8StringEncoding]
     options:@{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType,
               NSCharacterEncodingDocumentAttribute:[NSNumber numberWithInt:NSUTF8StringEncoding]}
     documentAttributes:NULL error:nil];

    [htmlAttributedStr addAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:KValueFontLblContent],
                                       NSForegroundColorAttributeName: [UIColor colorWithHexString:@"#555555"]
                                       }
                               range:NSMakeRange(0, htmlAttributedStr.length)];
    
    return htmlAttributedStr;
}



- (CGFloat)heightViewWithHtmlAttributedStr:(NSMutableAttributedString *)htmlAttributedStr
{
    CGSize textSize = [htmlAttributedStr boundingRectWithSize:(CGSize){KValueMaxWidthLblContent, CGFLOAT_MAX} options:NSStringDrawingUsesLineFragmentOrigin context:nil].size;
    CGFloat heightHtmlAttString = textSize.height;
        
    
    CGFloat marginBottom = 20;
    CGFloat viewHeightValue = heightHtmlAttString + marginBottom + [CPSCGoodsDetailsUIHelper heightCommonHeader];
    
    return viewHeightValue;
}

- (void)deBugUI
{
//    self.viewHtmlContentSuper.backgroundColor = kCPSCRandomColor;
//    self.lblContent.backgroundColor = kCPSCRandomColor;
}


#pragma mark - Getter Setter

- (UIView *)viewHtmlContentSuper
{
    if (!_viewHtmlContentSuper) {
        
        _viewHtmlContentSuper = [[UIView alloc] init];
        
        [_viewHtmlContentSuper addSubview:self.lblContent];
        [self.lblContent mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.top.left.bottom.right.mas_equalTo(0);
        }];
        
    }
    
    return _viewHtmlContentSuper;
}

- (UILabel *)lblContent
{
    if (!_lblContent) {
        
        _lblContent = [[UILabel alloc] init];
        _lblContent.textColor = [UIColor colorWithHexString:@"#EC5330"];
        _lblContent.font = [UIFont systemFontOfSize:KValueFontLblContent];
        _lblContent.textAlignment = NSTextAlignmentLeft;
        _lblContent.numberOfLines = 0;
        _lblContent.lineBreakMode = NSLineBreakByTruncatingTail;
        
    }
    
    return _lblContent;
}

@end
